<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style lang="scss">
        .tangram-suggestion-main {
            z-index: 9999999999;
        }
    </style>
    <!--v3.0版本的引用方式：src="http://api.map.baidu.com/api?v=3.0&ak=您的密钥"-->
    <script type="text/javascript"
            src="http://api.map.baidu.com/api?v=3.0&ak=vVaRzGOFPO9U1CHR92fOSeBPaCzyCCg9"></script>
</head>
<body class="hold-transition">
<div id="app">
    <div class="filter-container">
        <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                  class="filter-item" @keyup.enter.native="findPage1"></el-input>
        <el-button @click="findPage1()" class="dalfBut">查询</el-button>
        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
    </div>
    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
        <el-table-column type="index" align="center" label="序号"></el-table-column>
        <el-table-column prop="company_address" label="公司地址" align="center"></el-table-column>
        <el-table-column prop="longitude" label="所在经度" align="center"></el-table-column>
        <el-table-column prop="latitude" label="所在纬度" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
            <template slot-scope="scope">
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination-container">
        <el-pagination
                class="pagiantion"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
    </div>
    <!-- 新增标签弹层 -->
    <div class="add-form">
        <el-dialog title="新增地址" :visible.sync="dialogFormVisible" fullscreen="true">
            <el-row>
                <el-col :span="10">
                    <div id="r-result">
                        <el-input id="suggestId" v-model="address.company_address" style="width: 400px"/>
                    </div>
                    <div id="searchResultPanel"
                         style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                </el-col>
                <el-col :span="14">
                    <el-button type="primary" @click="handleAdd()">确定添加</el-button>
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                </el-col>
            </el-row>
            <div style="margin-top: 10px">
                <div id="container" style="height: 500px;width: 100%"></div>
            </div>
        </el-dialog>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<script type="text/javascript">
    new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 5,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            address: {
                company_address: null,//公司地址
                longitude: null,//经度
                latitude: null//纬度
            },
            dialogFormVisible: false,//增加表单是否可见
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },

        methods: {
            //添加
            handleAdd() {
                var myGeo = new BMap.Geocoder();
                // 将地址解析结果显示在地图上，并调整地图视野
                var _this = this;
                if (_this.address.company_address == '' || _this.address.company_address == null) {
                    _this.$message.error("地址不能为空");
                    return false;
                }
                myGeo.getPoint(_this.address.company_address, function (point) {
                        _this.address.longitude = point.lng;
                        _this.address.latitude = point.lat;
                        axios.post("/address/add.do", _this.address).then(
                            (response) => {
                                if (response.data.flag) {
                                    _this.$message({
                                        message: response.data.message,
                                        type: 'success'
                                    });
                                    _this.dialogFormVisible = false;
                                    _this.findPage();
                                    _this.resetForm();
                                } else {
                                    _this.$message.error(response.data.message);
                                }
                            })
                    },
                    "武汉市");
            },

            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.get("/address/findPage.do", {params: param}).then(
                    (response) => {
                        this.dataList = response.data.data.rows;
                        this.pagination.total = response.data.data.total;
                    }
                );
            },

            // 重置表单
            resetForm() {
                this.address = {
                    company_address: null,//公司地址
                    longitude: null,//经度
                    latitude: null//纬度
                };

            },


            // 弹出添加窗口
            handleCreate() {
                var _this = this;
                this.dialogFormVisible = true;
                this.$nextTick(() => {
                    // 百度地图API功能
                    function G(id) {
                        return document.getElementById(id);
                    }

                    // 创建地图实例
                    var map = new BMap.Map("container");
                    // 创建点坐标
                    var point = new BMap.Point(114.201, 30.555);
                    // 初始化地图，设置中心点坐标和地图级别
                    map.centerAndZoom(point, 13);
                    map.enableScrollWheelZoom(true);
                    map.addControl(new BMap.NavigationControl());
                    map.addControl(new BMap.ScaleControl());
                    map.addControl(new BMap.OverviewMapControl());
                    map.addControl(new BMap.MapTypeControl());
                    map.setCurrentCity("武汉"); // 仅当设置城市信息时，MapTypeControl的切换功能才能可用
                    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                        {
                            "input": "suggestId"
                            , "location": map
                        });
                    ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
                        var str = "";
                        var _value = e.fromitem.value;
                        var value = "";
                        if (e.fromitem.index > -1) {
                            value = _value.province + _value.city + _value.district + _value.street + _value.business;
                        }
                        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                        value = "";
                        if (e.toitem.index > -1) {
                            _value = e.toitem.value;
                            value = _value.province + _value.city + _value.district + _value.street + _value.business;
                        }
                        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                        G("searchResultPanel").innerHTML = str;
                    });

                    var myValue;
                    ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
                        var _value = e.item.value;
                        myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
                        G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                        setPlace();
                    });

                    function setPlace() {
                        map.clearOverlays();    //清除地图上所有覆盖物
                        function myFun() {
                            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                            map.centerAndZoom(pp, 18);
                            map.addOverlay(new BMap.Marker(pp));    //添加标注
                        }

                        var local = new BMap.LocalSearch(map, { //智能搜索
                            onSearchComplete: myFun
                        });
                        _this.address.company_address = myValue;
                        local.search(myValue);
                    }


                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            // 删除
            handleDelete(id) {
                this.$confirm("你确定要删除当前数据吗？", "提示", {//确认框
                    type: 'warning'
                }).then(() => {
                    //用户点击确定按钮，发送ajax请求，将检查项ID提交到Controller进行处理
                    axios.get("/address/del.do?id=" + id).then(
                        (response) => {
                            if (response.data.flag) {
                                this.$message({
                                    message: response.data.message,
                                    type: 'success'
                                });
                                this.findPage1();
                            } else {
                                this.$message.error(response.data.message);
                            }
                        }
                    );
                })
            },

            findPage1() {
                this.pagination.currentPage = 1;
                this.findPage();
            }
        }
    });


</script>
</html>